package com.ywgt.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywgt.system.pojo.Role;
import com.ywgt.system.mapper.RoleMapper;
import com.ywgt.system.pojo.User;
import com.ywgt.system.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywgt.system.service.UserService;
import com.ywgt.system.vo.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author yiping
 * @since 2021-01-18
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private UserService userService;

    @Resource
    private RoleMapper roleMapper;
    @Override
    public List<String> getRolesByUserName(String userName) {
        User user = userService.getUserByUsername(userName);
        List<Role> roles = getRoles(user.getId());
        List<String> roleList=new ArrayList<>();
        for (Role role : roles) {
            roleList.add(role.getRoleName());
        }
        return roleList;
    }
    @Override
    public List<Role> getRoles(Long id){
        List<Role> roles =roleMapper.getRolesByUserId(id);
        return roles;
    }

    @Override
    public Page<Role> findRoles(Long pageNum, Long pageSize, RoleVO roleVO) {
        Page<Role> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Role> wrapper = getWrapper(roleVO);
        Page<Role> rolePage = roleMapper.selectPage(page, wrapper);
        return rolePage;
    }


    public LambdaQueryWrapper<Role> getWrapper(RoleVO roleVO){
        LambdaQueryWrapper<Role> lambdaQueryWrapper=new LambdaQueryWrapper();
        if (!StringUtils.isEmpty(roleVO.getId())){
            lambdaQueryWrapper.eq(Role::getId,roleVO.getId());
        }
        if (!StringUtils.isEmpty(roleVO.getRoleName())){
            lambdaQueryWrapper.eq(Role::getRoleName,roleVO.getRoleName());
        }
        if (!StringUtils.isEmpty(roleVO.getCreateTime())){
            lambdaQueryWrapper.eq(Role::getCreateTime,roleVO.getCreateTime());
        }
        if (!StringUtils.isEmpty(roleVO.getRemark())){
            lambdaQueryWrapper.eq(Role::getRemark,roleVO.getRemark());
        }
        if (!StringUtils.isEmpty(roleVO.getModifiedTime())){
            lambdaQueryWrapper.eq(Role::getModifiedTime,roleVO.getModifiedTime());
        }
        if (!StringUtils.isEmpty(roleVO.getStatus())){
            lambdaQueryWrapper.eq(Role::getStatus,roleVO.getStatus());
        }
        return lambdaQueryWrapper;
    }
}
